from rest_framework.decorators import api_view, authentication_classes, permission_classes
from rest_framework.parsers import QueryDict
from django.shortcuts import render
from django.http import Http404
from rest_framework.views import APIView,Response
from rest_framework import status
from .models import Question,Choice,User
from .serializers import QuestionSerializer,QuestionDetailSerializer, UserProfileSerializer, UserProfileListSerializer
from first import serializers
from rest_framework.permissions import IsAuthenticated
# Create your views here.

class QuestionsList(APIView):
    '''
    查询系统中所有的问题
    '''

    def get(self,request,format=None):
        questions = [QuestionSerializer(question).data for question in Question.objects.all()]
        return Response({"questions":questions}) 

    def post(self,request,format=None):
        serializer = QuestionSerializer(data=request.data)
        if serializer.is_valid():
            serializer.save()
            return Response(serializer.data)
        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

class QuestionDetail(APIView):
    '''
    查询指定ID的QUESTION信息
    '''

    def get_object(self,id):
        try:
            return Question.objects.get(id=id)
        except Question.DoesNotExist:
            raise Http404

    def get(self,request,id,format=None):
        return Response(QuestionDetailSerializer(self.get_object(id)).data)

from .custom_authentication import AuthingIDAuthentication


class SelfProfileView(APIView):
    '''
    用户查看和修改个人资料
    '''
    authentication_classes = (AuthingIDAuthentication,)
    permission_classes = (IsAuthenticated,)

    def get(self, request):
        '''
        返回个人资料
        '''
        return Response(UserProfileSerializer(request.user).data)
    
    def patch(self, request):
        '''
        更改个人资料
        '''
        serializer = UserProfileSerializer(User.objects.get(id=request.user.id),data=request.data)
        if serializer.is_valid():
            serializer.save()
            return Response({'status':'success'})
        return Response(serializer.errors,status=status.HTTP_400_BAD_REQUEST)

@api_view(['GET'])
@authentication_classes([AuthingIDAuthentication])
@permission_classes([IsAuthenticated])
def users_profile_list(request):
    ITEM_PER_PAGE = 10
    page = request.query_params.get('page','1')
    page = int(page)
    #根据page算出起点与终点
    start_offset = (page-1) * ITEM_PER_PAGE
    #https://docs.djangoproject.com/zh-hans/4.1/topics/db/queries/
    users = User.objects.all()[start_offset:start_offset+ITEM_PER_PAGE]
    serializer = UserProfileListSerializer(users,many=True)
    return Response(serializer.data)

@api_view(['GET'])
@authentication_classes([AuthingIDAuthentication])
@permission_classes([IsAuthenticated])
def users_profile_detail(request,id):
    user:User
    try:
        user = User.objects.get(id=id)
    except User.DoesNotExist:
        return Response({"detail":"Not Found"},status=status.HTTP_404_NOT_FOUND)
    serializer = UserProfileSerializer(user)
    return Response(serializer.data)